package exercise.jslide.jslide.view.tools;

import exercise.jslide.jslide.command.MoveCommand;
import exercise.jslide.jslide.controller.PresentationController;
import exercise.jslide.jslide.model.SlideElement;
import exercise.jslide.jslide.app.AppContext;
import exercise.jslide.jslide.model.image.ImageElement;
import exercise.jslide.jslide.model.shapes.Shape;
import exercise.jslide.jslide.model.text.TextBox;
import exercise.jslide.jslide.utils.EdgePoint;
import exercise.jslide.jslide.view.EditorView;
import javafx.geometry.Point2D;
import javafx.scene.Cursor;

public class SelectTool implements Tool {
    private AppContext appContext;
    private SlideElement selectedElement;
    private double startX;
    private double startY;
    private EditorView editorView;
    private double initialX;
    private double initialY;

    public void setStartX(double startX) {
        this.startX = startX;
    }
    public void setStartY(double startY) {
        this.startY = startY;
    }
    public void setInitialX(double initialX) {
        this.initialX = initialX;
    }
    public void setInitialY(double initialY) {
        this.initialY = initialY;
    }





    public SelectTool(EditorView editorView,SlideElement selectedElement,double startX,double startY) {
        this.startX = startX;
        this.startY = startY;
        this.appContext = AppContext.getInstance();
        this.editorView = editorView;
        this.selectedElement = selectedElement;
        if (selectedElement != null) {
            if (selectedElement.getClass().getSuperclass() == Shape.class){
                appContext.getMainView().selectRibbonTab(3);
            }else if (selectedElement.getClass() == TextBox.class){
                appContext.getMainView().selectRibbonTab(4);
            }
        }
    }

    @Override
    public void activate() {
        // 切换鼠标光标为默认
        editorView.setCursor(Cursor.HAND);
    }

    @Override
    public void deactivate() {
        editorView.setCursor(Cursor.DEFAULT);
        if (selectedElement != null) {
            selectedElement.setSelected(false);
            selectedElement.setEditing(false);
            selectedElement = null;
            appContext.getMainView().updateView();
        }

    }

    @Override
    public void handleMousePressed(double x, double y) {
        startX = x;
        startY = y;

//        selectedElement = findElementAt(x, y);

        if (selectedElement != null) {
            if (selectedElement.getClass() == ImageElement.class){
                ImageElement imageElement = (ImageElement) selectedElement;
                imageElement.setEdgePoint(new Point2D(x,y));
            } else if (selectedElement.getClass().getSuperclass() == Shape.class){
                Shape shape = (Shape) selectedElement;
                shape.setEdgePoint(new Point2D(x,y));
            } else if (selectedElement.getClass() == TextBox.class){
                TextBox textBox = (TextBox) selectedElement;
                textBox.setEdgePoint(new Point2D(x,y));
            }
        }


//         查找并选择元素

    }

    @Override
    public void handleMouseDragged(double x, double y) {
        if (selectedElement != null) {
            double deltaX = x - startX;
            double deltaY = y - startY;

            selectedElement.setCurrentX(x);
            selectedElement.setCurrentY(y);

            if (selectedElement.getClass() == ImageElement.class){
                ImageElement imageElement = (ImageElement) selectedElement;
                if(imageElement.getEdgePoint()== EdgePoint.noPoint){
                    selectedElement.move(deltaX, deltaY);
                }
                else{
                    imageElement.reSize(deltaX,deltaY);
                }
            } else if (selectedElement.getClass().getSuperclass() == Shape.class){
                Shape shape = (Shape) selectedElement;
                if(shape.getEdgePoint()== EdgePoint.noPoint){
                    selectedElement.move(deltaX, deltaY);
                }
                else{
                    shape.reSize(deltaX,deltaY);
                }
            } else if (selectedElement.getClass() == TextBox.class){
                TextBox textBox = (TextBox) selectedElement;
                if(textBox.getEdgePoint()== EdgePoint.noPoint){
                    selectedElement.move(deltaX, deltaY);
                }
                else{
                    textBox.reSize(deltaX,deltaY);
                }
            } else{
                selectedElement.move(deltaX, deltaY);
            }




            startX = x;
            startY = y;

            // 请求视图重绘
            appContext.getMainView().updateView();
        }
    }

    @Override
    public void handleMouseReleased(double x, double y) {
        if (selectedElement != null) {
            MoveCommand moveCommand = new MoveCommand(selectedElement, initialX, initialY, selectedElement.getX(), selectedElement.getY());
            appContext.getUndoManager().executeCommand(moveCommand);

            appContext.getMainView().updateView();

        }
    }

    private SlideElement findElementAt(double x, double y) {
        // TODO: 实现元素查找
        return appContext.getCurrentPresentation()
                .getCurrentSlide()
                .findElementAt(x, y);
    }

    public SlideElement getSelectedElement() {
        return selectedElement;
    }


}